<template>
  <span class="screenshot">
    <el-tooltip class="item" effect="dark" :content="$t('feedback')" placement="left">
      <el-button @click="handleOk" type="primary" icon="el-icon-camera" circle></el-button>
    </el-tooltip>
    <el-dialog
      modal-append-to-body
      append-to-body
      :title="$t('feedback')"
      top="50px"
      :visible.sync="box"
      width="60%"
    >
      <el-form>
        <el-form-item prop="url" :model="form">
          <el-input placeholder="浏览器地址栏" readonly="true" v-model="form.url"></el-input>
        </el-form-item>
        <el-form-item prop="note">
          <el-input type="textarea" placeholder="请告诉我们遇到的问题或建议" v-model="form.note"></el-input>
        </el-form-item>
      </el-form>
      <div v-loading="flag" element-loading-text="正在截取屏幕数据" class="screenshot__img">
        <img :src="form.img" width="100%" />
      </div>
      <div slot="footer" class="screenshot__menu">
        <el-button type="primary" icon="el-icon-check" @click="handleSubmit">{{$t('submit')}}</el-button>
      </div>
    </el-dialog>
  </span>
</template>

<script>
import { baseUrl } from "@/config/env";
const urls = {
  save: baseUrl + "/suggestions/suggestions/add"
};

export default {
  data() {
    return {
      box: false,
      form: {
        img: "",
        note: "",
        url: window.location.href
      }
    };
  },
  computed: {
    flag() {
      return this.validatenull(this.form.img);
    }
  },
  methods: {
    handleSubmit() {
      axios.post(urls.save, this.form).then(res => {
        this.$message({ message: res.data.result, type: "success" });
        this.box = false;
      });
    },
    handleOk() {
      this.form = {
        img: "",
        note: "",
        url: window.location.href
      };
      this.$Screenshot(document.querySelector("#app")).then(canvas => {
        this.form.img = canvas.toDataURL("image/jpeg", 0.8);
      });
      this.box = true;
    }
  }
};
</script>

<style lang="scss" scoped>
.screenshot {
  position: fixed;
  bottom: 50px;
  right: 20px;
  z-index: 2048;
  &__img {
    min-height: 200px;
    margin-top: 50px;
    padding: 10px 20px;
    box-sizing: border-box;
    border: 1px solid #eee;
    width: 100%;
  }
  &__menu {
    text-align: center;
  }
}
</style>